package com.dodoke.controller;

import com.dodoke.beans.User;
import com.dodoke.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.JstlView;

// 标识 UserController 是一个组件，这个标识能有在 spring 启动的时候，让 spring 扫描到他
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired//它可以对类成员变量、方法及构造函数进行标注，让 spring 完成 bean 自动装配的工作
    private UserService userService;

    @RequestMapping("/login")
    public String toLogin() {
        return "login";
    }

    @GetMapping("/index")
    public String toIndex() {
        return "index";
    }

    @PostMapping("/login")
    public String login(User user) {
        boolean flag = userService.login(user);
        if (flag) {
            return "redirect:catalog_list";
        }
        return "redirect:login";
    }

}
